/*
 * =====================================================================================
 *
 *       Filename:  MediaRecordTask.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  04/22/2015 02:59:26 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Dellinger.Zhang (mn), rainxy2000@hotmail.com
 *        Company:  TD studio
 *
 * =====================================================================================
 */
#include "MediaRecordTask.h"
#include "def.h"
#include "MediaRecordCommandIn.h"
#include "json/json.h"

MediaRecordTask::MediaRecordTask (
		ctTCPTaskPool *pool, 
		const int sock, 
		const struct sockaddr_in *addr)
	: ctTCPTask(pool, sock, addr, true, true),
	  server_type_(0),
	  server_id_(0)
{}

bool
MediaRecordTask::verify(const Cmd::t_NullCmd *pcmd)
{
	using namespace Cmd::MediaRecord;
	const stLoginCmd *logcmd = static_cast<const stLoginCmd *>(pcmd);
	if (CMDIN_LOGIN		== logcmd->wCmd &&
		SCMDIN_LOGIN	== logcmd->wSubCmd &&
		MCU_SERVER_TYPE	== logcmd->dest_type_)
	{
		server_type_	= logcmd->server_type_;
		server_id_		= logcmd->server_id_;
		return true;
	}
	return false;
}

int 
MediaRecordTask::verifyConn()
{
	int ret = mSocket.recvToBuf_NoPoll();
	if (ret > 0)
	{
		BYTE pstrCmd[ctSocket::MAX_DATASIZE];
		int nCmdLen = mSocket.recvToCmd_NoPoll(pstrCmd,sizeof(pstrCmd));
		if (nCmdLen <= 0) {
			return 0;
		}
		else {
			using namespace Cmd;
			t_NullCmd *pNullCmd = reinterpret_cast<t_NullCmd *>(pstrCmd);
			if (verify(pNullCmd))
				return 1;
			else
				return -1;
		}
	}
	return ret;
}

int 
MediaRecordTask::waitSync()
{
    return 1;
}

void 
MediaRecordTask::addToContainer()
{
//    RECORD_MGR().checkRecording();
}

void 
MediaRecordTask::removeFromContainer()
{
}

bool 
MediaRecordTask::uniqueAdd()
{
    //return MediaRecordTaskManager::getSingleton().uniqueAdd(this);
}

bool 
MediaRecordTask::uniqueRemove()
{
    //return MediaRecordTaskManager::getSingleton().uniqueRemove(this);
}

bool
MediaRecordTask::msgParse(const Cmd::t_NullCmd *pcmd,
						  const DWORD 			nCmdLen)
{
	using namespace Cmd::MediaRecord;
	switch(pcmd->wCmd) 
	{
		case CMDIN_ROOM:
			return msgParse_room(pcmd, nCmdLen);
			break;
		default:
			break;
	}
	Zebra::logger->error("%s(%u,%u,%u)", __PRETTY_FUNCTION__,
			pcmd->wCmd, pcmd->wSubCmd, nCmdLen);
	return false;
}

bool
MediaRecordTask::msgParse_room(const Cmd::t_NullCmd *pcmd,
							   const DWORD			 nCmdLen)
{
	using namespace Cmd::MediaRecord;
	switch(pcmd->para)
	{
		case SCMDIN_RECORDING_SPEAKING_LIST_NOTIFY:
			{
				const stRoomRecordingSpeakingListNotify *rev = static_cast<const stRoomRecordingSpeakingListNotify *>(pcmd);
				std::string root_str(rev->json_, rev->size_);

				Json::Reader reader;
				Json::Value root;
				if (!reader.parse(root_str, root)) {
					Zebra::logger->error("json parse error ---stRoomRecordingSpeakingListNotify");
					return false;
				}

				for (size_t i = 0; i < root.size(); i++) {                                        
					Json::Value speaker_info = root[i];
					DWORD speaker_id = speaker_info["speaker_id"].asUInt();
					Zebra::logger->info("Add speaker id(%u), roomid(%u) by list", speaker_id, rev->room_id_);
					//VOIPManager::getSingleton().setUserSpeak(rev->room_id_, speaker_id, true); 
					CT_TRACE("#### room(%u) speaker list, user: %d", rev->room_id_, speaker_id);
				}
				return true;
			}
			break;
		case SCMDIN_RECORDING_ADD_SPEAKING_NOTIFY:
			break;
		case SCMDIN_RECORDING_DEL_SPEAKING_NOTIFY:
			break;
		case SCMDIN_RECORDING_REQ_RECORD_ADDR:
			break;
		case SCMDIN_RECORDING_OPEN_BOARD_NOTIFY:
			break;
		case SCMDIN_RECORDING_CLOSE_BOARD_NOTIFY:
			break;
		case SCMDIN_RECORDING_TURN_PAGE_NOTIFY:
			break;
		default:
			break;
	}
	Zebra::logger->error("%s(%u,%u,%u)", __PRETTY_FUNCTION__, pcmd->cmd, pcmd->para, nCmdLen);
	return false;
}
